//
//  ComposeToolbar.m
//  随拍
//
//  Created by lanou3g on 16/1/19.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "ComposeToolbar.h"

@implementation ComposeToolbar

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        // 初始化按钮
        [self setupBtn:@"23" highImage:@"23" typ:ComposeToolbarButtonTypeCamera];
        
        [self setupBtn:@"23" highImage:@"23" typ:ComposeToolbarButtonTypePicture];
        
        [self setupBtn:@"23" highImage:@"23" typ:ComposeToolbarButtonTypeMention];
        
        [self setupBtn:@"23" highImage:@"23" typ:ComposeToolbarButtonTypeEmotion];
        
        [self setupBtn:@"23" highImage:@"23" typ:ComposeToolbarButtonTypeTrend];
        
    }
    return self;
}

/**
 * 创建一个按钮
 */
- (void)setupBtn:(NSString *)image highImage:(NSString *)highImage typ:(ComposeToolbarButtonType)typ
{
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = typ;
    [self addSubview:btn];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置所有按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat btnW = self.width / count;
    CGFloat btnH = self.height;
    for (NSUInteger i = 0; i<count; i++) {
        UIButton *btn = self.subviews[i];
        btn.y = 0;
        btn.width = btnW;
        btn.x = i * btnW;
        btn.height = btnH;
    }
}

- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickButton:)]) {
        //        NSUInteger index = (NSUInteger)(btn.x / btn.width);
        [self.delegate composeToolbar:self didClickButton:btn.tag];
    }
}



@end
